//
//  VCOfClickYongHu.m
//  SeeTheWorld
//
//  Created by dllo on 16/3/1.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "VCOfClickYongHu.h"
#import "PCH.pch"
#import "CellOfClickYongHu.h"
#import "HeaderOfClickYongHu.h"
#import "ModelOfClickYongHuHeader.h"
#import "ModelOfClickYongHu.h"
#import <UIImageView+WebCache.h>


@interface VCOfClickYongHu () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arrayOfModel;
@property (nonatomic, retain)ModelOfClickYongHuHeader *model;
@property (nonatomic, retain)UIButton *buttonOfLeft;

@end

@implementation VCOfClickYongHu

- (void)dealloc {
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    [_buttonOfLeft release];
    [_model release];
    [_arrayOfModel release];
    [_tableView release];
    [super dealloc];
}


- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.buttonOfLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfLeft.frame = CGRectMake(5, 10, 30, 30);
    [self.buttonOfLeft addTarget:self action:@selector(fanHui:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonOfLeft setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-2"] forState:UIControlStateNormal];
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithCustomView:self.buttonOfLeft];
    self.navigationItem.leftBarButtonItem = bar;
    [bar release];
    
    self.navigationItem.title = self.name;
    self.arrayOfModel = [NSMutableArray array];
    
#pragma mark - 解析数据
    NSString *str = [NSString stringWithFormat:@"http://chanyouji.com/api/users/%@.json?page=1", self.id];
    [NetworkingHandler networkingWithURL:str completeHandle:^(NSData *data,id result) {
        
        self.model = [[ModelOfClickYongHuHeader alloc] init];
        self.model.name = [result objectForKey:@"name"];
        self.model.image = [result objectForKey:@"image"];
        self.model.trips_count = [result objectForKey:@"trips_count"];
        
        for (NSDictionary *dic in [result objectForKey:@"trips"]) {
            ModelOfClickYongHu *model1 = [[ModelOfClickYongHu alloc] init];
            [model1 setValuesForKeysWithDictionary:dic];
            [self.arrayOfModel addObject:model1];
            [model1 release];
            
        }
     
        [self.tableView reloadData];
        [_model release];
       
    }];
    
#pragma mark - 创建tableView
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[CellOfClickYongHu class] forCellReuseIdentifier:@"pool"];
    [self.tableView registerClass:[HeaderOfClickYongHu class] forHeaderFooterViewReuseIdentifier:@"pool1"];
    
    self.tableView.bounces = NO;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    HeaderOfClickYongHu *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"pool1"];
    
    
    [view passModel:self.model];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 150;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrayOfModel.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CellOfClickYongHu *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];

    ModelOfClickYongHu *model = [self.arrayOfModel objectAtIndex:indexPath.row];
    
    [cell passModel:model];
    
    return cell;
}

- (void)fanHui:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
